package com.jiangyg.mall.product.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangyg.mall.db.mybatis.BaseServiceImpl;
import com.jiangyg.mall.core.support.snowflake.SnowflakeBuilder;
import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.utils.bean.BeanUtils;
import com.jiangyg.mall.product.constant.ColumnConstant;
import com.jiangyg.mall.product.dto.AttrDTO;
import com.jiangyg.mall.product.entity.Attr;
import com.jiangyg.mall.product.entity.AttrAttrgroup;
import com.jiangyg.mall.product.entity.Attrgroup;
import com.jiangyg.mall.product.vo.AttrVO;
import com.jiangyg.mall.product.dao.AttrAttrgroupDao;
import com.jiangyg.mall.product.dao.AttrDao;
import com.jiangyg.mall.product.dao.AttrgroupDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AttrServiceImpl extends BaseServiceImpl<AttrDao, Attr> implements AttrService {

    private final AttrgroupDao attrgroupDao;

    private final AttrAttrgroupDao attrAttrgroupDao;

    @Autowired
    public AttrServiceImpl(AttrDao attrDao,
                           AttrgroupDao attrgroupDao,
                           AttrAttrgroupDao attrAttrgroupDao) {
        super(attrDao);
        this.attrgroupDao = attrgroupDao;
        this.attrAttrgroupDao = attrAttrgroupDao;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Pagination<AttrVO> selectPagination(Pagination<AttrVO> pagination) throws Exception {
        pagination = this.executeSelectPagination(pagination, AttrVO.class);
        final List<AttrVO> records = pagination.getRecords();
        // 查询分组ID
        {
            final List<String> attrIds = records.stream().map(AttrVO::getId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(attrIds)) {
                return pagination;
            }
            final QueryWrapper<AttrAttrgroup> wrapper = new QueryWrapper<>();
            wrapper.in(ColumnConstant.ATTR_ID, attrIds);
            final List<AttrAttrgroup> attrAttrgroups = this.attrAttrgroupDao.selectList(wrapper);
            records.forEach(r -> {
                final Iterator<AttrAttrgroup> iterator = attrAttrgroups.iterator();
                while (iterator.hasNext()) {
                    final AttrAttrgroup attrAttrgroup = iterator.next();
                    if (StringUtils.equals(r.getId(), attrAttrgroup.getAttrId().toString())) {
                        r.setAttrgroupId(attrAttrgroup.getAttrgroupId().toString());
                        iterator.remove();
                    }
                }
            });
        }
        // 查询分组名称
        {
            final List<String> attrgroupIds = records.stream().map(AttrVO::getAttrgroupId).distinct().collect(Collectors.toList());
            final QueryWrapper<Attrgroup> wrapper = new QueryWrapper<>();
            wrapper.in(ColumnConstant.ID, attrgroupIds);
            final List<Attrgroup> attrgroups = this.attrgroupDao.selectList(wrapper);
            records.forEach(r -> attrgroups.forEach(a -> {
                if (StringUtils.equals(r.getAttrgroupId(), a.getId().toString())) {
                    r.setAttrgroupName(a.getName());
                }
            }));
        }
        return pagination;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(AttrDTO dto) throws Exception {
        Attr attr;
        final Long id = dto.getId();
        // 修改属性信息
        if (id != null && (attr = this.baseMapper.selectById(id)) != null) {
            BeanUtils.copyProperties(attr, dto);
            return this.baseMapper.updateById(attr) > 0;
        }
        // 新增属性
        attr = new Attr();
        BeanUtils.copyProperties(attr, dto);
        attr.setId(SnowflakeBuilder.nextId());
        // 新增属性和属性组关联
        final Long attrgroupId = dto.getAttrgroupId();
        if (attrgroupId != null) {
            final AttrAttrgroup attrAttrgroup = new AttrAttrgroup();
            attrAttrgroup.setAttrId(attr.getId());
            attrAttrgroup.setAttrgroupId(attrgroupId);
            this.attrAttrgroupDao.insert(attrAttrgroup);
        }
        return this.baseMapper.insert(attr) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public AttrVO selectById(Long attrId) throws Exception {
        final AttrVO attr = this.executeSelectById(attrId, AttrVO.class);
        if (attr != null) {
            final QueryWrapper<AttrAttrgroup> wrapper = new QueryWrapper<>();
            wrapper.eq(ColumnConstant.ATTR_ID, attrId);
            final AttrAttrgroup attrAttrgroup = this.attrAttrgroupDao.selectOne(wrapper);
            if (attrAttrgroup != null) {
                attr.setAttrgroupId(attrAttrgroup.getAttrgroupId().toString());
            }
        }
        return attr;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Long> attrIds) {
        return this.baseMapper.deleteBatchIds(attrIds) > 0;
    }

}
